<?php

namespace app\modules\group\service;

use Yii;
use app\common\base\CommonService;
use app\modules\group\models\Group;
use app\helpers\DatetimeHelper;

class GroupService extends CommonService
{
    function __construct()
    {
        // 对父类service(component)初始化，并指定model
        parent::init();
        $this->model = new Group();
    }

    /**
     * 创建群组
     */
    public function createGroup($ownerId, $groupName)
    {
        $info = $this->info([
            'condition' => [
                'ownerId' => $ownerId,
                'groupName' => $groupName,
            ]
        ]);

        if (!empty($info)) {
            return parent::error('ERROR_GROUP_EXIST', '群组已存在');
        }

        $this->model->groupName = $groupName;
        $this->model->ownerId = $ownerId;
		$this->model->users = \json_encode([$ownerId]);
		$this->model->create_timestamp = DatetimeHelper::msectime();
		$this->model->update_timestamp = DatetimeHelper::msectime();
		$this->model->save();

        return [
			'success' => 1,
		];
    }

    /**
     * 删除群组
     */
    public function deleteGroup($ownerId, $groupName)
    {
        $info = $this->info([
            'condition' => [
                'ownerId' => $ownerId,
                'groupName' => $groupName,
            ]
        ]);

        if (empty($info)) {
            return parent::error('ERROR_GROUP_NOT_EXIST', '群组不存在');
        }

        $this->delete([
            'ownerId' => $ownerId,
            'groupName' => $groupName,
        ]);

        return [
			'success' => 1,
		];
    }

    /**
     * 获取所有群组信息
     */
    public function listGroup($ownerId)
    {
        $info = $this->lists([
            'condition' => [
                'ownerId' => $ownerId,
            ]
        ]);

        $groups = [];
        foreach ($info as $group) {
            $data = [];
            $data['id'] = $group['id'];
            $data['name'] = $group['groupName'];
            array_push($groups, $data);
        }

        return [
            'success' => 1,
            'data' => $groups,
        ];
    }

    /**
     * 获得users字段
     */
    public function getUsers($groupId)
    {
        $info = $this->info([
            'condition' => [
                'id' => $groupId,
            ]
        ]);

        if (empty($info)) {
            return parent::error('ERROR_GROUP_NOT_EXIST', '群组不存在');
        }

        $res = \json_decode($info['users']);

        return [
            'success' => 1,
            'data' => $res
        ];
    }

    /**
     * 添加群组成员
     */
    public function addUserToJson($groupId, $add_userId)
    {
        $info = $this->info([
            'condition' => [
                'id' => $groupId,
            ]
        ]);

        $json = \json_decode($info['users']);
        array_push($json, intval($add_userId));
        $res = \json_encode($json);

        $this->update([
            'users' => $res,
        ],[
            'id' => $groupId,
        ]);

        return [
            'success' => 1
        ];
    }

    /**
     * 删除群组成员
     */
    public function dropUserFromJson($groupId, $drop_userId)
    {
        $info = $this->info([
            'condition' => [
                'id' => $groupId,
            ]
        ]);

        $json = \json_decode($info['users']);
        $json = array_diff($json, [intval($drop_userId)]);
        $res = \json_encode($json);

        $this->update([
            'users' => $res,
        ],[
            'id' => $groupId,
        ]);

        return [
            'success' => 1
        ];
    }

    /**
     * 数据转换接口
     */
    public function ownerIdGroupNameToGroupId($ownerId, $groupName)
    {
        $info = $this->info([
            'condition' => [
                'ownerId' => $ownerId,
                'groupName' => $groupName,
            ]
        ]);

        if (!$info) {
            return parent::error('ERROR_GROUP_NOT_EXIST', '群组不存在');
        } else {
            return $info['id'];
        }
    }
}